# 打开一个新窗口

from PyQt5.QtCore import Qt
from PyQt5.Qt import QMainWindow, QApplication, QCursor
from config import Cfg


class NewWindow(QMainWindow):
    def __init__(self):
        super(NewWindow, self).__init__()

        desktop = QApplication.desktop()
        screen_rect = desktop.screenGeometry()
        # 显示屏分辨率大小
        height = screen_rect.height()
        width = screen_rect.width()

        self.mouse = False

        self.h_ratio = Cfg.dic['winh'] / height
        self.w_ratio = Cfg.dic['winw'] / width

        self.init_ui()

        # 关闭时销毁窗口
        self.setAttribute(Qt.WA_DeleteOnClose, False)

    def init_ui(self):
        # 设置无边框窗口
        self.setWindowFlags(Qt.FramelessWindowHint)

    def setLoc(self, x, y):
        self.move(x, y)

    # 设置移动窗口
    def mousePressEvent(self, event):
        if event.button() == Qt.LeftButton:
            # 获取鼠标相对位置
            self.mouse_position = event.globalPos() - self.pos()
            if self.mouse_position.x() < int(self.w_ratio * 50) or self.mouse_position.y() < int(self.h_ratio * 50):
                self.mouse = True
                event.accept()
                # 更改鼠标图标
                self.setCursor(QCursor(Qt.OpenHandCursor))

    def mouseMoveEvent(self, event):
        if Qt.LeftButton and self.mouse:
            # 更改窗口位置
            self.move(event.globalPos() - self.mouse_position)
            event.accept()

    def mouseReleaseEvent(self, event):
        self.mouse = False
        self.setCursor(QCursor(Qt.ArrowCursor))


if __name__ == '__main__':
    import sys

    app = QApplication(sys.argv)
    win = NewWindow()
    win.show()
    app.exit(app.exec_())
